	var InputComment = "";

$(document).ready(function(){
	
	$("#exampleInputEmail2").focus();
	
	// ดึงข้อมูล หมายเหตุการอ่านน้ำ
	getComment();
	
	// เมื่อแก้ไข เลขอ่านมาตรครั้งปัจจุบัน
	$('#currentReadNumber').keyup(function(){
		var consumption;
		var currentRead = $("#currentReadNumber").val();
		var previousRead = $("#previousReadNumber").val();
		consumption = parseFloat(currentRead) -  parseFloat(previousRead);
		$("#consumption").val(consumption);
    });
	
	// เมื่อแก้ไข เลขอ่านมาตรครั้งปัจจุบัน
	$('#previousReadNumber').keyup(function(){
		var consumption;
		var currentRead = $("#currentReadNumber").val();
		var previousRead = $("#previousReadNumber").val();
		consumption = parseFloat(currentRead) -  parseFloat(previousRead);
		$("#consumption").val(consumption);
    });
	
	// เมื่อแก้ไข ค่าน้ำ
	$('#waterCharge').keyup(function(){
		calTextChargeService();
	});
	
	//เมื่อแก้ไข ค่าบริการ
	$('#waterFee').keyup(function(){
		calTextChargeService();
	});
	
	//เมื่อแก้ไข ค่าภาษี
	$('#vatAmount').keyup(function(){
		var billAmount;
		var totalAmount;
		var waterCharge = $("#waterCharge").val();
		var waterFee = $("#waterFee").val();
		var vatAmount = $("#vatAmount").val();
		var garbageCharge = $("#garbageCharge").val();
		var wasteWaterCharge = $("#wasteWaterCharge").val();
		var otherCharge = $("#otherCharge").val();
		billAmount = parseFloat(waterCharge.replace(",", "")) + parseFloat(waterFee.replace(",", "")) + parseFloat(vatAmount.replace(",", ""));
		totalAmount = parseFloat(garbageCharge) + parseFloat(wasteWaterCharge) + parseFloat(otherCharge) + parseFloat(billAmount);
		$("#billAmount").val(billAmount);
		$("#totalAmount").val(totalAmount);
	});
	
	// เมื่อแก้ไข ค่าเก็บขยะ
	$('#garbageCharge').keyup(function(){
		$("#totalAmount").val(calTextCharge($('#garbageCharge').val(), "GC"));
	});
	
	// เมื่อแก้ไข ค่าบำบัดน้ำเสีย
	$('#wasteWaterCharge').keyup(function(){
		$("#totalAmount").val(calTextCharge($('#wasteWaterCharge').val(), "WC"));
	});
	
	// เมื่อแก้ไข ค่าอื่นๆ
	$('#otherCharge').keyup(function(){
		$("#totalAmount").val(calTextCharge($('#otherCharge').val(), "OC"));
	});
});

// ดึงข้อมูล หมายเหตุการอ่านน้ำ
function getComment() {
	$.getJSON('CS34000Srvl', {methodType : "getComment", url_address:$("#url_address").val(), agent_browser:$("#agent_browser").val()}, function(result) {
		var options = '';
		options += '<option value="">-- เลือกหมายเหตุ --</option>';
		for (var i = 0; i < result.values.length; i++) {
			var selected="";
			if(InputComment!=""&&InputComment==result.values[i]){
				selected ='selected="selected"';
			}
			options += '<option value="' + result.values[i] + '" ' + selected + '>'
					+ result.messages[i] + '</option>';
		}
		$("select#InputComment").html(options);
	});
}

//เมื่อกดปุ่ม Enter กรณีค้นหา ทะเบียนผู้ใช้น้ำ
function Enter(e) {
	if (e.keyCode == 13) {
		searchAccount();
	  return false;  
	}
};

// Function ค้นหา ทะเบียนผู้ใช้น้ำ
function searchAccount(){
	var account = $("#exampleInputEmail2").val();
	if (account == ""){
		$('#failAccountModal').modal();
	} else {
		$.ajax({           	
    		url : "CS34000Srvl",
    		type : "POST",
    		data : {methodType: "getDataAR", account:account, url_address:$("#url_address").val(), agent_browser:$("#agent_browser").val()},
    		async : false ,
    		success : function(data) {
    			var newdata = jQuery.parseJSON(data);
    			if (newdata.message == "resultNull"){
    				$('#messageBoxSearch').text("ไม่พบข้อมูลทะเบียนผู้ใช้น้ำนี้");
    				$('#failResultModal').modal();
    			} else if (newdata.message == "status"){
    				$('#messageBoxSearch').text("ไม่พบข้อมูลทะเบียนผู้ใช้น้ำนี้ เนื่องจาก AR ใบแรกถูกยกเลิกแล้ว");
    				$('#failResultModal').modal();
    			} else if (newdata.message == "receiveDate"){
    				$('#messageBoxSearch').text("ไม่พบข้อมูลทะเบียนผู้ใช้น้ำนี้ เนื่องจาก ออกใบเสร็จแล้ว");
    				$('#failResultModal').modal();
    			} else if (newdata.message == "printBillNumber"){
    				$('#messageBoxSearch').text("ไม่พบข้อมูลทะเบียนผู้ใช้น้ำนี้ เนื่องจาก พิมพ์ใบแจ้งหนี้แล้ว");
    				$('#failResultModal').modal();
    			} else {
        			$("#billNumber").text(newdata.billNumber);
        			$("#zoneName").text(newdata.zoneName);
        			$("#address").text(newdata.address);
        			$("#routeDescription").text(newdata.routeDescription);
        			$("#sequence").text(newdata.sequence);
        			$("#account").text(newdata.account);
        			$("#fullName").text(newdata.fullName);
        			$("#customerClass").text(newdata.customerClass);
        			$("#customerClassId").val(newdata.customerClassId);
        			if (newdata.allowance == "1"){
        				$("#allowance").text("มีส่วนลดทหารผ่านศึก");
        			} else {
        				$("#allowance").text("");
        			}
        			$("#datetimepicker1").val(newdata.previousReadDate);
        			$("#datetimepicker2").val(newdata.currentReadDate);
        			$("#previousReadNumber").val(newdata.previousReadNumber);
        			$("#currentReadNumber").val(newdata.currentReadNumber);
        			$("#consumption").val(newdata.consumption);
        			$("#waterCharge").val(newdata.waterCharge);
        			$("#waterFee").val(newdata.waterFee);
        			$("#vatAmount").val(newdata.vatAmount);
        			$("#billAmount").val(newdata.billAmount);
        			$("#garbageCharge").val(newdata.garbageCharge);
        			$("#wasteWaterCharge").val(newdata.wasteWaterCharge);
        			$("#otherCharge").val(newdata.otherCharge);
        			$("#totalAmount").val(newdata.totalAmount);
        			
        			InputComment = newdata.commentCode;
        			getComment();
    			}
    		}
		});	
	}
}

// Function ยืนยันการแก้ไขข้อมูลแก้ไขการอ่านมาตร
function confirm(){
	var account = $("#account").text();
	if (account == "") {
		$('#checkResultModal').modal();
	} else {
		$('#confirmEditModal').modal();
	}
}

// Function บันทึกการแก้ไขข้อมูลแก้ไขการอ่านมาตร 
function save(){
	var account = $("#account").text();
	var consumption = $("#consumption").val();
	var previousReadDate = $("#datetimepicker1").val();
	var currentReadDate = $("#datetimepicker2").val();
	var previousReadNumber = $("#previousReadNumber").val();
	var currentReadNumber = $("#currentReadNumber").val();
	var waterCharge = $("#waterCharge").val();
	var waterFee = $("#waterFee").val();
	var vatAmount = $("#vatAmount").val();
	var billAmount = $("#billAmount").val();
	var garbageCharge = $("#garbageCharge").val();
	var wasteWaterCharge = $("#wasteWaterCharge").val();
	var otherCharge = $("#otherCharge").val();
	var totalAmount = $("#totalAmount").val();
	var comment = $("#InputComment").val();
	$.ajax({           	
		url : "CS34000Srvl",
		type : "POST",
		data : {methodType: "updateData", 
			account:account,
			consumption:consumption,
			previousReadDate:previousReadDate,
			currentReadDate:currentReadDate,
			previousReadNumber:previousReadNumber,
			currentReadNumber:currentReadNumber,
			waterCharge:waterCharge,
			waterFee:waterFee,
			vatAmount:vatAmount,
			billAmount:billAmount,
			garbageCharge:garbageCharge,
			wasteWaterCharge:wasteWaterCharge,
			otherCharge:otherCharge,
			totalAmount:totalAmount,
			comment:comment,
			url_address:$("#url_address").val(),
			agent_browser:$("#agent_browser").val()
		},
		async : false ,
		success : function(data) {
			var newdata = jQuery.parseJSON(data);
			if (newdata.message == "success") {
				$('#confirmEditModal').modal('hide');
				$('#exampleInputEmail2').val("");
				$('#successModal').modal();
			} else {
				$('#confirmEditModal').modal('hide');
				$('#failModal').modal();
			}
			
		} 
	});
}

// Function คำนวณค่าน้ำทั้งหมด
function calConsumption(){
	var account = $("#account").text();
	var typeCal = $("input:radio[name=typeCal]:checked").val();
	var consumption = $("#consumption").val();
	var previousReadDate = $("#datetimepicker1").val();
	var currentReadDate = $("#datetimepicker2").val();
	var customerClass = $("#customerClassId").val();
	var allowance = "";
	if ($("#allowance").text() != ""){
		allowance = "1";
	} else {
		allowance = "0";
	}
	$.ajax({           	
		url : "CS34000Srvl",
		type : "POST",
		data : {
			methodType:"calConsumption", 
			account:account,
			consumption:consumption,
			typeCal:typeCal, 
			previousReadDate:previousReadDate,
			currentReadDate:currentReadDate, 
			customerClass:customerClass,
			allowance:allowance,
			url_address:$("#url_address").val(),
			agent_browser:$("#agent_browser").val()
		},
		async : false ,
		success : function(data) {
			var newdata = jQuery.parseJSON(data);
				$("#consumption").val(newdata.consumption);
				$("#waterCharge").val(newdata.waterChargeNew);
				calTextChargeService();
			}
	});
}

// Function เมื่อแก้ไข ค่าน้ำ หรือค่าบริการ
function calTextChargeService(){
	var billAmount;
	var totalAmount;
	var waterCharge = $("#waterCharge").val();
	var waterFee = $("#waterFee").val();
	var garbageCharge = $("#garbageCharge").val();
	var wasteWaterCharge = $("#wasteWaterCharge").val();
	var otherCharge = $("#otherCharge").val();
	
	$.ajax({           	
		url : "CS34000Srvl",
		type : "POST",
		data : {methodType: "getVatRate", waterCharge:waterCharge, waterFee:waterFee, url_address:$("#url_address").val(), agent_browser:$("#agent_browser").val()},
		async : false ,
		success : function(data) {
			var newdata = jQuery.parseJSON(data);
			billAmount = parseFloat(waterCharge.replace(",", "")) + parseFloat(waterFee.replace(",", "")) + parseFloat(newdata.vatAmountNew.replace(",", ""));
			totalAmount = parseFloat(garbageCharge) + parseFloat(wasteWaterCharge) + parseFloat(otherCharge) + parseFloat(billAmount);
			$("#vatAmount").val(newdata.vatAmountNew);
			$("#billAmount").val(format(billAmount,2));
			$("#totalAmount").val(format(totalAmount,2));
		}
	});	
}

// Function แกไข ค่าเก็บขยะ ค่าบำบัดน้ำเสีย ค่าอื่นๆ
function calTextCharge(value1, value2){
	var billAmount;
	var totalAmount;
	var garbageCharge;
	var wasteWaterCharge;
	var otherCharge;
	if (value2 == "GC"){
		garbageCharge = value1;
	} else {
		garbageCharge = $("#garbageCharge").val();
	}
	if (value2 == "WC") {
		wasteWaterCharge = value1;
	} else {
		wasteWaterCharge = $("#wasteWaterCharge").val();
	}
	if(value2 == "OC"){
		otherCharge = value1;
	} else {
		otherCharge = $("#otherCharge").val();
	}
	billAmount = $("#billAmount").val();
	totalAmount = parseFloat(garbageCharge.replace(",", "")) + parseFloat(wasteWaterCharge.replace(",", "")) + parseFloat(otherCharge.replace(",", "")) + parseFloat(billAmount.replace(",", ""));
	
	return format(totalAmount,2);
}

// รูปแบบ ตัวเลข
function format(num, fix) {
    var p = num.toFixed(fix).split(".");
    return p[0].split("").reduceRight(function(acc, num, i, orig) {
        if ("-" === num && 0 === i) {
            return num + acc;
        }
        var pos = orig.length - i - 1
        return  num + (pos && !(pos % 3) ? "," : "") + acc;
    }, "") + (p[1] ? "." + p[1] : "");
}

// Function เคลียร์หน้าจอ
function clearScreen(){
	$("#billNumber").text("");
	$("#zoneName").text("");
	$("#address").text("");
	$("#routeDescription").text("");
	$("#sequence").text("");
	$("#account").text("");
	$("#fullName").text("");
	$("#customerClass").text("");
	$("#address").text("");
	$("#allowance").text("");
	$("#datetimepicker1").val("");
	$("#datetimepicker2").val("");
	$("#previousReadNumber").val("");
	$("#currentReadNumber").val("");
	$('input[name=typeCal][value=G]').prop('checked', 'checked');
	$("#consumption").val("0");
	$("#waterCharge").val("0.00");
	$("#waterFee").val("0.00");
	$("#vatAmount").val("0.00");
	$("#billAmount").val("0.00");
	$("#garbageCharge").val("0.00");
	$("#wasteWaterCharge").val("0.00");
	$("#otherCharge").val("0.00");
	$("#totalAmount").val("0.00");
}

//Function ปิด Popup ค้นหา
function getFocus(){
	$(".close").trigger('click');
	$('#exampleInputEmail2').focus();
	clearScreen();
}

//Function ปิด Popup แก้ไขสำเร็จ
function getFocus2(){
	$(".close").trigger('click');
	$('#exampleInputEmail2').focus();
	clearScreen();
	refresh();
}

//Function ปิด Popup ยืนยัน
function cancel(){
	$(".close").trigger('click');
	$('#consumption').focus();
}

// เริ่มใหม่
function refresh(){
	location.reload();
}

